/*
 * ListaPacientePanel.java *
 * Created on 07/11/2010, 09:37:15
 */
package clinicapoo.view.paciente;

import clinicapoo.factory.Services;
import clinicapoo.model.Paciente;
import clinicapoo.view.dialog.Dialogs;

import java.util.Vector;
import java.util.List;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/** *
 * @author Christiano
 */
public class ListaPacientePanel extends javax.swing.JInternalFrame implements TableModelListener
{
    Paciente objPac = new Paciente();
    private List<Paciente> lista;

    /** Creates new form ListaPacientePanel */
    public ListaPacientePanel()
    {
        initComponents();
        listar();
//        tblPaciente.getModel().addTableModelListener(this);
    }

    @Override
    public void tableChanged(TableModelEvent event)
    {
        try
        {
            int row = event.getFirstRow();
            int column = event.getColumn();
            TableModel model = (TableModel) event.getSource();
            String data = model.getValueAt(row, column).toString();
            Paciente p = this.lista.get(row);

            switch (column)
            {
                case 1: p.setNome(data); break;
                case 2: p.setCpf(data); break;
                case 3: p.setTelefone(data); break;
                case 4: p.setAddress(data); break;
            }

            Services.paciente.alterar(p);
        }
        catch (Exception e)
        {
            Dialogs.showError(this, e);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblPaciente = new javax.swing.JTable();
        btnFechar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();

        setTitle("Pacientes Cadastrados no Sistema");

        tblPaciente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Nome", "CPF", "fone", "Endereço"
            }
        ));
        tblPaciente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblPacienteMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblPaciente);

        btnFechar.setText("fechar");
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });

        btnExcluir.setText("excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnAlterar.setLabel("editar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(234, Short.MAX_VALUE)
                        .addComponent(btnAlterar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExcluir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnFechar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFechar)
                    .addComponent(btnExcluir)
                    .addComponent(btnAlterar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFecharActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnFecharActionPerformed

    private void tblPacienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblPacienteMouseClicked
     try
        {
            int rowIndex = tblPaciente.getSelectedRow();

            if (rowIndex < 0)
            {
                return;
            }
            Paciente p = lista.get(rowIndex);
        }
        catch (Exception e)
        {
            JOptionPane.showInternalMessageDialog(this, e.toString());
        }

    }//GEN-LAST:event_tblPacienteMouseClicked

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
       excluirPaciente();
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
      //  atualizarDados();
      
    }//GEN-LAST:event_btnAlterarActionPerformed

    public void atualizarDados()
    {
        try
        {
            int rowIndex = tblPaciente.getSelectedRow();

            if (rowIndex < 0)
            {
                return;
            }
            int celColuna = tblPaciente.getEditingColumn();
            int celLinha = tblPaciente.getEditingRow();
            Paciente p = lista.get(rowIndex);

            String nome = String.valueOf(tblPaciente.getValueAt(rowIndex, celColuna));
            p.setNome(nome);
            Services.paciente.alterar(p);
        }
        catch (Exception e)
        {
            JOptionPane.showInternalMessageDialog(this, e.toString());
        }
    }

    public void listar()
    {
        lista = Services.paciente.listar();
        Vector<String> tableHeaders = new Vector<String>();
        Vector tableData = new Vector();

        tableHeaders.add("CPF");
        tableHeaders.add("Nome");
        tableHeaders.add("Fone");
        tableHeaders.add("End");

        for (Paciente p : lista)
        {
            Vector<Object> oneRow = new Vector<Object>();
            oneRow.add(p.getCpf());
            oneRow.add(p.getNome());
            oneRow.add(p.getAddress());
            oneRow.add(p.getTelefone());
            tableData.add(oneRow);
        }

        tblPaciente.setModel(new DefaultTableModel(tableData, tableHeaders));
    }

    private void excluirPaciente()
    {
        try
        {
            int rowIndex = tblPaciente.getSelectedRow();

            if (rowIndex < 0)
            {
                return;
            }
            if (JOptionPane.showConfirmDialog(this, "excluir?", "atençao", 0) != JOptionPane.OK_OPTION)
            {
                return;
            }

            Paciente p = lista.get(rowIndex);
            Services.paciente.excluir(p);
            listar();
            JOptionPane.showMessageDialog(this, "Dados apagados");

        }
        catch (Exception e)
        {
            JOptionPane.showInternalMessageDialog(this, e.toString());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnFechar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblPaciente;
    // End of variables declaration//GEN-END:variables
}
